FROM {{ namespace }}/{{ image_prefix }}base:{{ tag }}
{% block labels %}
LABEL maintainer="{{ maintainer }}" name="{{ image_name }}" build-date="{{ build_date }}"
{% endblock %}
{% if base_package_type == 'rpm' and base_arch != 'x86_64' %}
LABEL fluentd_version="0.12" fluentd_binary="fluentd"
    {% set fluentd_user = 'fluentd' %}
{% elif base_package_type == 'deb' and base_arch != 'x86_64' %}
LABEL fluentd_version="0.14" fluentd_binary="fluentd"
    {% set fluentd_user = 'fluentd' %}
{% else %}
LABEL fluentd_version="0.14" fluentd_binary="td-agent"
    {% set fluentd_user = 'td-agent' %}
{% endif %}

{% block fluentd_header %}{% endblock %}

{% import "macros.j2" as macros with context %}

{{ macros.enable_extra_repos(['td-agent']) }}

{% if base_package_type == 'rpm' %}

    {% set fluentd_packages = [
        'gcc-c++',
        'make'
    ] %}

    {% if base_arch == 'x86_64' %}
        {% set fluentd_packages = fluentd_packages + [
            'td-agent'
        ] %}
    {% else %}
        {% set fluentd_packages = fluentd_packages + [
            'fluentd',
            'ruby-devel',
            'rubygem-fluent-plugin-elasticsearch',
            'rubygem-fluent-plugin-grok-parser',
            'rubygem-fluent-plugin-rewrite-tag-filter'
        ] %}
    {% endif %}

{% elif base_package_type == 'deb' %}

    {% set fluentd_packages = [
        'g++',
        'make'
    ] %}

    {% if base_arch == 'x86_64' %}
        {% set fluentd_packages = fluentd_packages + [
            'td-agent'
        ] %}
    {% else %}
        {% set fluentd_packages = fluentd_packages + [
            'ruby',
            'ruby-dev'
        ] %}
    {% endif %}

{% endif %}

{{ macros.configure_user(name=fluentd_user, groups='mysql') }}
{{ macros.install_packages(fluentd_packages | customizable("packages")) }}

# Distro specific files and operations
# Fluentd on rpm-based non-x86_64 is installed from rpm
{% if base_package_type == 'deb' and base_arch != 'x86_64' %}
RUN /usr/bin/gem install fluentd --no-rdoc --no-ri
{% endif %}

COPY fluentd_sudoers /etc/sudoers.d/kolla_fluentd_sudoers

RUN chmod 440 /etc/sudoers.d/kolla_fluentd_sudoers \
    && mkdir -p /etc/{{ fluentd_user }} \
    && mkdir -p /var/run/{{ fluentd_user }} \
    && chown -R {{ fluentd_user }}: /etc/{{ fluentd_user }} /var/run/{{ fluentd_user }}

COPY extend_start.sh /usr/local/bin/kolla_extend_start
RUN chmod 755 /usr/local/bin/kolla_extend_start

{% block fluentd_plugins_install %}

{% if not (base_package_type == 'rpm' and base_arch != 'x86_64') %}
    {% set fluentd_plugins = [
        'fluent-plugin-elasticsearch',
        'fluent-plugin-grep',
        'fluent-plugin-grok-parser',
        'fluent-plugin-parser',
        'fluent-plugin-rewrite-tag-filter',
    ] %}
{% endif %}

# NOTE(hrw): one of plugins fetches 'string-scrub' which tries to use
# /usr/bin/mkdir directly while in Debian it is in /bin
{% if base_distro == 'debian' and base_arch == 'x86_64' %}
RUN ln -s /bin/mkdir /usr/bin/mkdir
{% endif %}

{{ macros.install_fluent_plugins(fluentd_user, fluentd_plugins | customizable("plugins")) }}

{% endblock %}

{% block fluentd_monasca_plugin_install %}
# Build and install Fluentd output plugin for Monasca API
ARG monasca_output_plugin_tag=1.0.0
ARG monasca_output_plugin_url=https://github.com/monasca/fluentd-monasca/archive/$monasca_output_plugin_tag.tar.gz

RUN curl -sSL $monasca_output_plugin_url -o /tmp/fluentd-monasca.tar.gz \
    && tar -xvf /tmp/fluentd-monasca.tar.gz -C /tmp \
    && cd /tmp/fluentd-monasca-* \
{%- if fluentd_user == 'fluentd' %}
    && gem build fluentd-monasca-output.gemspec \
{%- else %}
    && td-agent-gem build fluentd-monasca-output.gemspec \
{%- endif %}
    && {{ macros.install_fluent_plugins(fluentd_user, ['fluentd-monasca-output-*.gem'], chain=True) }} \
    && rm -rf /tmp/fluentd*

{% endblock %}

{% block fluentd_footer %}{% endblock %}
{% block footer %}{% endblock %}

USER {{ fluentd_user }}
